package com.splant.android.smart.UILevel.Community.Services;

import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

import com.splant.android.smart.ModelLevel.Device;
import com.splant.android.smart.ProtocolLevel.CRC16;
import com.splant.android.smart.SystemDefinedLevel.Services.UdpService;
import com.splant.android.smart.SystemDefinedLevel.Utils.System_DataType_Util;

public class sPlantUdpService extends UdpService {

   private Device device;
    public void setDevice(Device device)
    {
        this.device=device;
    }
    @Override
    public void handleRecvMsg(byte[] recvBytes) {

        if (recvBytes.length == 5) {

            int state = System_DataType_Util.byteToInt(recvBytes, 1);
            if (recvHandler!=null)
                recvHandler.obtainMessage(2, state).sendToTarget();

        } else {

            if (CRC16.checkCRC(recvBytes) && recvBytes[0] == -86 && recvBytes.length > 20) {

                byte[] deviceIdBytes = System_DataType_Util.getPartBytes(recvBytes, 3, 18);
                String deviceId = System_DataType_Util.bytesToHexString(deviceIdBytes, "");

                if (recvHandler!=null&&device != null && device.getDevicesid().equals(deviceId))
                    recvHandler.obtainMessage(3, recvBytes).sendToTarget();

            }
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new sPlantUdpService.sPlantUdpBinder();
    }

    public class sPlantUdpBinder extends Binder {

        public sPlantUdpService getService(){
            return sPlantUdpService.this;
        }
    }
}
